
var $body =  $("body");
var paramStr;

//保存的是添加对对比的测点的pi地址和测点名
var addPoint;
var addServer;
$(function(){
	$("#startTime").jeDate({
	    isinitVal:true,
	    initAddVal:{hh:"-8"},
	    format:"YYYY-MM-DD hh:mm:ss",
	    zIndex:3000,
	    maxDate:$.nowDate({})
	});
	$("#endTime").jeDate({
	    isinitVal:true,
	    initAddVal:{},
	    format:"YYYY-MM-DD hh:mm:ss",
	    zIndex:3000,
	    maxDate:$.nowDate({})
	});
	
	//阻止鼠标的右击事件
	document.oncontextmenu = function(e) {
		e.preventDefault();
	}
	
	window.onclick = function(e) {
		hideRMenu();
	}
	
	$("text[type='tagTextVal']").mousedown(function(e){
		if(e.which==3) {
			addPoint = $(this).attr("pointname");
			addServer = $(this).attr("piservice");
			showRMenu("", e.clientX, e.clientY);
		}
	})
		
	//鼠标停留在测点0.2秒后才会调用测点的气泡事件（显示最大值，最小值，均值），反之不会调用
		var hoverTime;
		$("text[type='tagTextVal']").hover(function() {
			var obj = this;
			var $obj = $(this);
			hoverTime = setTimeout(function() {
				var title = document.createElementNS("http://www.w3.org/2000/svg", "title");
				$.ajax({
					type:'post',
					url:ctx + "/svgStatic/getValueInfo",
					data:{ 
						"point": $obj.attr('pointname'),
						"servers": $obj.attr('piservice'),
						"startTime": $("#startTime").val(), 
						"endTime": $("#endTime").val(), 
					},
					dataType:'json',
					success:function(data){
						var text = document.createTextNode(data);
						title.appendChild(text);obj.appendChild(title);
					}
				});
				
			},200);
		},function() {
			clearTimeout(hoverTime);
		})
	
	var params = [];
		
	// 界面初始化展示
	$('text').each(function(){
		var element = $(this);
		var point = element.attr('pointname');
		var servers = element.attr('piservice');
		
		if(typeof(point) == undefined || null== point || '' == point){
		}else{
			params.push({"point":point, "servers":servers});
		}
	});
	
	paramStr = JSON.stringify(params);
	
	query();
});

function query() {
	
	$.ajax({
		type:'post',
		url:ctx + "/svgStatic/getValue",
		data:{"paramStr": paramStr, "startTime":$("#startTime").val(), 
			"endTime":$("#endTime").val(),"time":function(){ return new Date().getTime();}},
		success:function(data){
			var reObj = JSON.parse(data);
			//把svg图的别名改为实时值
			$.each(reObj, function(idx, obj){
				//显示测点值
				$("text[pointname='"+ obj.point + "'][piservice='"+obj.server+"']").each(function(indesx, textObj){
					var value;
					//判断保留几位小数
					if($(textObj).attr("pbnumberformat")=="0.0") {
						value = Math.round(obj.value*10)/10;
					}else if($(textObj).attr("pbnumberformat")=="0.00") {
						value = Math.round(obj.value*100)/100;
					}else if($(textObj).attr("pbnumberformat")=="General") {
						value = obj.value;
					}else {
						value = obj.value;
					}
					
					textObj.textContent = value;	
				});
				
			});
			
		},
		error:function(){},
	    complete: function(){
	    }
	});
}


var opened;
function showMessage(obj){
	if(null != opened){
		opened.close();
	}
	var point= $(obj).attr('pointname');
	var servers = $(obj).attr('piservice');
	
	var startTime = $("#startTime").val();
	var endTime = $("#endTime").val();
	
	var url = ctx + "/svgStatic/displayPic?point="+point+"&servers="+servers
			+"&startTime="+startTime+"&endTime="+endTime;
	opened = window.open(url,"svgwindow","top=200,left=400,height=500,width=1300,toolbar=no,scrollbars=yes,menubar=no,titlebar=no,location=no,status=no"); 
}

//svg图之间的跳转
function jump(obj) {
	window.location.href = ctx + '/svgStatic/staticTime?file=' +　obj;
}

//新增测点对比项
function addContrast(piservice,pointname) {
	$.ajax({
		url:ctx+"/svgStatic/isExist",
		type:"post",
		data:{point:pointname,servers:piservice},
		success:function(data){
			if(data) {
				showSuccessMessage("新增对比测点成功");
				hideRMenu();
			}else {
				showErrorMessage("新增对比测点失败！");
			}
		}
	})
}

//显示右键菜单
function showRMenu(type, x, y) {
    $('#rMenu').css({"top":y+20+"px", "left":x+20+"px", "visibility":"visible"}); //设置右键菜单的位置、可见
}
//隐藏右键菜单
function hideRMenu() {
    if ($('#rMenu')) $('#rMenu').css({"visibility": "hidden"}); //设置右键菜单不可见
}

//时间戳转成日期字符串
function formatDateTime(inputTime) {
	var date = new Date(inputTime);
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	m = m < 10 ? ('0' + m) : m;
	var d = date.getDate();
	d = d < 10 ? ('0' + d) : d;
	var h = date.getHours();
	h = h < 10 ? ('0' + h) : h;
	var minute = date.getMinutes();
	var second = date.getSeconds();
	minute = minute < 10 ? ('0' + minute) : minute;
	second = second < 10 ? ('0' + second) : second;
	return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
};

